package cn.formaggie.controller.manage;

import cn.formaggie.annotation.CheckTwiceSubmit;
import cn.formaggie.controller.BaseController;
import cn.formaggie.entity.model.PageBean;
import cn.formaggie.entity.model.sample.Sample;
import cn.formaggie.entity.model.sample.SampleImage;
import cn.formaggie.service.face.SampleImageService;
import cn.formaggie.service.face.SampleService;
import cn.formaggie.service.face.SampleTypeService;
import cn.formaggie.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/sample")
public class SampleManageController extends BaseController {

    @Autowired
    private SampleService sampleService;

    @Autowired
    private SampleTypeService sampleTypeService;

    @Autowired
    private SampleImageService sampleImageService;

    @RequiresPermissions({"样品管理"})
    @RequestMapping("/list")
    public ModelAndView list(@ModelAttribute(STATE_MSG)String stateMsg){
        ModelAndView mav=new ModelAndView();
        mav.addObject(STATE_MSG,stateMsg);
        mav.addObject("types",sampleTypeService.getAllSampleTypes());
        mav.addObject("pageName","样品管理");
        mav.addObject("mainPage",BACK_PAGE_PATH+"/manager/sample/list.jsp");
        mav.setViewName("/backpage/backTemp");
        return mav;
    }

    @RequiresPermissions({"样品管理"})
    @RequestMapping("/load")
    @ResponseBody
    public Map load(@RequestParam(value = "s_code",required = false)String code,
                    @RequestParam(value = "s_sampleName",required = false)String sampleName,
                    @RequestParam(value = "s_typeId",required = false)Integer typeId,
                    @RequestParam(value = "page") Integer page,
                    @RequestParam(value = "limit") Integer limit){
        PageBean pageBean=new PageBean(page,limit);
        Map<String,Object> sqlMap=new HashMap<>();
        sqlMap.put("code",code);
        sqlMap.put("sampleName",sampleName);
        sqlMap.put("typeId",typeId);
        sqlMap.put("pageBean",pageBean);

        List<Sample> data=sampleService.findSamples(sqlMap);
        Integer count=sampleService.getSamplesCount(sqlMap);

        Map<String,Object> viewMap=new HashMap<>();
        viewMap.put("data",data);
        viewMap.put("count",count);
        viewMap.put("code",0);
        viewMap.put("msg","");
        return viewMap;
    }
    @RequiresPermissions({"样品管理"})
    @PostMapping("/loadImages")
    @ResponseBody
    public List<SampleImage> loadImages(Integer id){
        List<SampleImage> sampleImages=sampleImageService.getSampleImagesBySampleId(id);
        return  sampleImages;
    }

    @RequiresPermissions({"样品管理"})
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String,Object> upload(HttpServletRequest request,
                                     @RequestParam("sampleId") Integer sampleId,
                                     @RequestParam("images") MultipartFile[] images){

        String rootRath=request.getServletContext().getRealPath("/")+"static\\sampleImages";
        Map<String,Object> viewMap=new HashMap<>();
        List<String> data=new ArrayList<>();
        for(MultipartFile file:images){
            String originalFileName=file.getOriginalFilename();
            String fileName= DateUtil.getCurrentDateStr()+originalFileName.substring(originalFileName.lastIndexOf("."));
            try {
                String url=rootRath+"/"+fileName;
                file.transferTo(new File(url));
                sampleImageService.addImages(sampleId,"/static/sampleImages/"+fileName);
                data.add("/static/sampleImages/"+fileName);
            } catch (IOException e) {
                viewMap.put("errno",1);
            }
        }

        viewMap.put("errno",0);
        viewMap.put("data",data);
        return viewMap;
    }


    @RequiresPermissions({"样品管理"})
    @RequestMapping("/add")
    @CheckTwiceSubmit
    public String addSample(Sample sample, RedirectAttributes model){
        Boolean state=sampleService.addSample(sample);
        model.addFlashAttribute(STATE_MSG,state?"添加成功":"添加失败");
        return "redirect:/back/sample/list";
    }

    @RequiresPermissions({"样品管理"})
    @RequestMapping("/modifyInfo")
    @CheckTwiceSubmit
    public String modifySampleInfo(Sample sample, RedirectAttributes model){
        Boolean state=sampleService.modifyInfo(sample);
        model.addFlashAttribute(STATE_MSG,state?"修改成功":"修改失败");
        return "redirect:/back/sample/list";
    }

    @RequiresPermissions({"样品管理"})
    @RequestMapping(value = "/delete" ,method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteSample(@RequestParam("id") Integer id){
        return sampleService.deleteSample(id);
    }

}
